package com.cxhome.cxinfox.cxuser;

import java.util.List;

import com.cxhome.cxinfox.customer.CustomerService;
import com.cxhome.cxinfox.cxdepartment.DepartmentService;
import com.jfinal.plugin.activerecord.Page;
import com.traffic.managent.annotation.ControllerBind;
import com.traffic.managent.base.CxBaseController;
import com.traffic.managent.model.system.Role;
import com.traffic.managent.model.system.User;
import com.traffic.managent.model.system.UserInfo;
import com.traffic.managent.util.SecurityPbkdf2Utils;
import com.traffic.managent.util.StringUtils;

/**
 * 用户管理
 */
@ControllerBind(controllerKey = "/system/cxuser")
public class UserController extends CxBaseController {

	private static final String path = "/pages/system/cxuser/cxuser_";

	public void index() {
		list();
	}

	public void list() {

		User model = getModelByAttr(User.class);
		String orderSql = " order by ";
		// 排序
		String orderBy = getBaseForm().getOrderBy();
		if (StringUtils.isEmpty(orderBy)) {
			orderSql += " u.createTime desc";
		} else {
			orderSql += orderBy;
		}
		Page<User> page = UserService.service.paginate(model, getPaginator(), orderSql);

		setAttr("page", page);
		setAttr("attr", model);
		render(path + "list.html");
	}

	/**
	 * 跳转添加页面
	 */
	public void add() {
		setAttr("selectParentDepartments", new DepartmentService().selectDepart(getPara()));
		setAttr("selectCustomer", new CustomerService().selectCustomer(""));
		render(path + "add.html");
	}

	/**
	 * 跳转编辑页面
	 */
	public void edit() {
		User user = User.dao.findById(getPara());
		UserInfo cxuserInfo = UserInfo.dao.findById(user.getStr("userinfoids"));
		setAttr("selectParentDepartments", DepartmentService.service.selectDepart(user.getStr("departmentId")));
		setAttr("selectCustomer", new CustomerService().selectCustomer(user.getStr("customerId")));

		setAttr("user", user);
		setAttr("userInfo", cxuserInfo);
		render(path + "edit.html");
	}

	/**
	 * 保存用户信息
	 */
	public void save() {

		String ids = getPara();
		String username = getSessionUser().getUserName();
		User usermodel = getModel(User.class, "user");

		UserInfo userinfomodel = getModel(UserInfo.class, "userInfo");

		String result = UserService.service.saveOrUpdate(usermodel, userinfomodel, ids, username);
		renderMessage(result);
	}

	/**
	 * 查看用户详情
	 */
	public void view() {
		User user = User.dao.findById(getPara());
		UserInfo cxuserInfo = UserInfo.dao.findById(user.getStr("userinfoids"));
		setAttr("user", user);
		setAttr("userInfo", cxuserInfo);
		String type = getSessionUser().getStr("TYPE");
		setAttr("isReset", "0".equals(type) ? "0" : "1");

		render(path + "view.html");
	}

	/**
	 * 删除当前用户信息
	 */
	public void delete() {
		UserService.service.delete(getPara());
		list();
	}

	/**
	 * 跳转到授权角色页面
	 * 
	 */
	public void auth() {
		String userid = getPara();
		List<Role> list = Role.dao.findByWhere(" where 1=1 ");
		// 获取所有角色
		String roleids = UserService.service.getRoleids(userid);
		setAttr("ids", userid);
		setAttr("roleids", roleids);
		// 根结点
		setAttr("list", list);
		render(path + "auth.html");
	}

	/**
	 * 保存角色信息
	 * 
	 */
	public void auth_save() {
		String userid = getPara("ids");
		String roleids = getPara("roleids");
		UserService.service.saveAuth(userid, roleids, getSessionUser().getUseriD());
		renderMessage("保存成功");
	}

}
